//
//  EWxmlNode.m
//  mkMapTest0
//
//  Created by 鱼 bin on 12-6-5.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "EWxmlNode.h"


@implementation EWxmlNode
+(id)xmlNode:(EWxmlDocument*)doc{
	return [[[EWxmlNode alloc] initWithDocument:doc] autorelease];
}

-(id)initWithDocument:(EWxmlDocument*)doc{
	self = [super init];
	document = doc;
	isRootNode = NO;
	return self;
}

+(id)xmlRootWithPath:(NSString*)path{
	return [[[EWxmlNode alloc] initRootWithPath:path] autorelease];
}
-(id)initRootWithPath:(NSString*)path{
	self = [super init];
	document =[[EWxmlDocument alloc] initWithContentOfPath:path];
	[self setRoot];
	return self;
}

-(void)collectAtrributes:(NSString*)key{
	//提取属性，并存入表
	if (arributes != nil) {
		[arributes release];
		arributes = nil;
	}
	arributes = [[NSMutableDictionary alloc] initWithCapacity:2];
	int attriRawDataBegin = dataBegin;
	NSString* keyStart = [NSString stringWithFormat:@"<%@",key]; 
	int keyStartState = 0;
	for (int i = dataBegin; i < dataEnd; i++) {
		NSRange charRange = {i,1};
		NSString* dataChar = [[document getXmlDataStr] substringWithRange:charRange];
		
		charRange.location = keyStartState;
		charRange.length = 1;
		NSString* keyChar = [keyStart substringWithRange:charRange];
		
		if (keyStartState >= key.length) {
			//找到属性表起始位置
			attriRawDataBegin = i + 1;
			break;
		}
		
		if ([dataChar isEqualToString:keyChar]){
			keyStartState++;
		}else {
			keyStartState = 0; //还原
		}
	}
	NSRange charRange = {attriRawDataBegin,1};
	charRange.length = 1;
	NSString* dataChar = [[document getXmlDataStr] substringWithRange:charRange];
	//NSLog(@"testc:%@  i:%d",dataChar,attriRawDataBegin);
	
	//向后查找，直到 ">"
	NSMutableString* elem = [NSMutableString stringWithString:@""];
	NSMutableArray* elemArray = [NSMutableArray arrayWithCapacity:2];
	int j = attriRawDataBegin;
	while (YES) {
		
		//以空格和“=”分隔
		if (![dataChar isEqualToString:@" "] && ![dataChar isEqualToString:@"="] && ![dataChar isEqualToString:@">"]) {
			[elem appendString:dataChar];
		}else {
			if (elem.length > 0) {
				[elemArray addObject:elem];
				//NSLog(@"elem:%@",elem);
				elem = [NSMutableString stringWithString:@""];
			}
			
		}
		if ([dataChar isEqualToString:@">"]) {
			break;
		}
		charRange.location = ++j;
		dataChar = [[document getXmlDataStr] substringWithRange:charRange];
	}
	if (elemArray.count%2 != 0) {
		NSLog(@"解析属性 %@ 出错  elemCount:%d",key,elemArray.count);
	}
	for (int i = 0; i < elemArray.count; i = i + 2) {
		NSString* _attriKey = [elemArray objectAtIndex:i];
		NSString* _attriValue = [elemArray objectAtIndex:i + 1];
		// value有引号则去除引号
		charRange.location = 0;
		charRange.length = 1;
		
		if ([[_attriValue substringWithRange:charRange] isEqualToString:@"\""]) {
			charRange.location = 1;
			charRange.length = _attriValue.length - 2;
			_attriValue = [_attriValue substringWithRange:charRange];
		}
		
		[arributes setObject:_attriValue forKey:_attriKey];
	}
}
-(NSMutableDictionary*)getAtrributes{
	if (arributes == nil) {
		NSLog(@"warning! please collect it with a key!");
	}
	return arributes;
}

-(BOOL)chr:(NSString*)chr In:(NSArray*)strArray{
	BOOL isIn = NO;
	for (NSString* str in strArray) {
		if ([str isEqualToString:chr]) {
			isIn = YES;
			break;
		}
	}
	return isIn;
}


-(NSMutableArray*)splitContentWith:(NSString*)sep{
	NSMutableArray* resultArray = [NSMutableArray arrayWithCapacity:20];
	NSMutableString* elem = [NSMutableString stringWithString:@""];
	NSArray* seps = [NSArray arrayWithObjects:@"\n",@"\r",sep,nil];
	//NSLog(@"testLen:%d",elem.length);
	for (int i = dataBegin; i < dataEnd; i++) {
		NSRange charRange = {i,1};
		NSString* dataChar = [[document getXmlDataStr] substringWithRange:charRange];
		
		if (![self chr:dataChar In:seps]) {
			[elem appendString:dataChar];
			//NSLog(@"datachar2:%@",dataChar);
		}else {
			if (elem.length > 0 &&![elem isEqualToString:@""]) {
				[resultArray addObject:elem];
				//NSLog(@"elem:%@",elem);
				elem = [NSMutableString stringWithString:@""];
			}
			
		}
	}
	return resultArray;
}

-(NSMutableArray*)getSubNodes:(NSString*)key{
	//获得子节点
	//先找<key  >的末位置
	//再找</key>的初位置
	//当然是从begin到end
	//[self collectAtrributes:key];
	NSString* keyStart = [NSString stringWithFormat:@"<%@>",key]; 
	NSString* keyStop = [NSString stringWithFormat:@"</%@>",key]; 
	
	NSMutableArray* keyStartPArray = [NSMutableArray arrayWithCapacity:10];
	NSMutableArray* keyStopPArray = [NSMutableArray arrayWithCapacity:10];
	
	int keyStartState = 0;
	int keyStopState = 0;
	NSString* secondChar = nil;
	BOOL canBreak = YES;  //能打断
	for (int i = dataBegin; i < dataEnd; i++) {
		NSRange charRange = {i,1};
		NSString* dataChar = [[document getXmlDataStr] substringWithRange:charRange];
		
		if ([dataChar isEqualToString:@"<"]) {
			NSRange range = {i + 1,1};
			secondChar = [[document getXmlDataStr] substringWithRange:range];
			keyStartState = 0;
			canBreak = YES;
		}
		
		
		charRange.location = keyStartState;
		charRange.length = 1;
		NSString* keyChar = [keyStart substringWithRange:charRange];
		
		if ( (![dataChar isEqualToString:keyChar]) &&keyStartState>0 && canBreak) {
			keyStartState = 0; //被打断了
			//NSLog(@"break i :%d  %@  %@",i,dataChar,keyChar);
		}
		
		if ([dataChar isEqualToString:keyChar] && ![secondChar isEqualToString:@"/"]) {
			if (keyStartState >= keyStart.length - 2) {
				//字符检测完毕，不能被打断了
				canBreak = NO;
			}
			keyStartState++;
			if (keyStartState == keyStart.length) {
				//检测到一个keyStart,记录i
				[keyStartPArray addObject:[NSNumber numberWithInt:i + 1]];
				keyStartState = 0; //状态复位，查下一个
				canBreak = YES;
			}
		}
	}
	
	int keyStartP = -1;
	for (int i = dataBegin; i < dataEnd; i++) {
		NSRange charRange = {i,1};
		NSString* dataChar = [[document getXmlDataStr] substringWithRange:charRange];
		if ([dataChar isEqualToString:@"<"]) {
			NSRange range = {i + 1,1};
			secondChar = [[document getXmlDataStr] substringWithRange:range];
			keyStartP = i;
			keyStopState = 0;
		}
		charRange.location = keyStopState;
		charRange.length = 1;
		NSString* keyChar = [keyStop substringWithRange:charRange];
		if ([dataChar isEqualToString:keyChar] && [secondChar isEqualToString:@"/"]) {
			keyStopState++;
			if (keyStopState == keyStop.length) {
				//检测到一个keyStart,记录i
				[keyStopPArray addObject:[NSNumber numberWithInt:keyStartP]];
				keyStopState = 0; //状态复位，查下一个
			}
		}
	}
	//NSLog(@"debug0:%d",keyStartPArray.count);
	if (keyStartPArray.count != keyStopPArray.count) {
		NSLog(@"解析 %d  到  %d  出错,<%@>键不匹配",key);
		return nil;
	}
	NSMutableArray* subNodeArray = [NSMutableArray arrayWithCapacity:10];
	for(int i = 0;i < keyStartPArray.count;i++){
		int _begin = [[keyStartPArray objectAtIndex:i] intValue];
		int _end = [[keyStopPArray objectAtIndex:i] intValue];
		EWxmlNode* _node = [EWxmlNode xmlNode:document];
		[_node setDataBegin:_begin];
		[_node setDataEnd:_end];
		[subNodeArray addObject:_node];
	}
	return subNodeArray;
}

-(EWxmlDocument*)getDocument{
	return document;
}
-(int)getDataBegin{
	return dataBegin;
}
-(int)getDataEnd{
	return dataEnd;
}
-(void)setDataBegin:(int)begin{
	dataBegin = begin;
}
-(void)setDataEnd:(int)end{
	dataEnd = end;
}
-(void)setDocument:(EWxmlDocument*)doc{
	document = doc;
}
-(int)getDataLength{
	return dataEnd - dataBegin;
}
-(NSString*)getNodeContentString{
	NSRange range;
	range.location = dataBegin;
	range.length = [self getDataLength];
	NSString* content = [[document getXmlDataStr] substringWithRange:range];
	return content;
}
-(void)setRoot{
	isRootNode = YES;
	nodeContentHeaderBegin = 0;
	nodeContentHeaderEnd = [[document getXmlDataStr] length];
	dataBegin = 0;
	dataEnd = [[document getXmlDataStr] length];
}

-(void)dealloc{
	if (isRootNode) {
		[document release];
	}
	[arributes release];
	[super dealloc];
}

@end
